package com.ruoqing.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoqing.core.constant.Constants;
import com.ruoqing.core.dto.BaseResponse;
import com.ruoqing.core.dto.user.UserResponse;
import com.ruoqing.core.enums.UserQueryEnum;
import com.ruoqing.user.dao.UserDao;
import com.ruoqing.user.service.api.UserService;
import com.ruoqing.core.domain.rquser.UserEntity;
import com.ruoqing.utils.common.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
	* @author YaoXian
	* @title: UserServiceImpl
	* @projectName ruoxi-blog
	* @description: 若兮微服务 - 用户服务UserService接口实现类
	* @date 2020-11-03 21:27
	*/
@RestController
@Slf4j
public class UserServiceImpl implements UserService {
		
		@Resource
		private UserDao userDao;
		
		@Resource
		private UserService userService;
		
		public UserEntity findUserByCondition(String queryParam, UserQueryEnum userQueryEnum) {
				QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<UserEntity>();
				if (!StringUtils.isEmpty(queryParam)){
						switch (userQueryEnum){
								case username:
										queryWrapper.eq("ruoqing_user_username",queryParam);
										break;
								case email:
										queryWrapper.eq("ruoqing_user_email",queryParam);
										break;
								case phoneNumber:
										queryWrapper.eq("ruoqing_user_phonenumber",queryParam);
										break;
								case qqNumber:
										queryWrapper.eq("ruoqing_user_qqnumber",queryParam);
										break;
								case weChatNumber:
										queryWrapper.eq("ruoqing_user_wechat",queryParam);
										break;
								default:
										log.info("无效精确查询用户参数"+userQueryEnum);
						}
				}
				return	userDao.selectOne(queryWrapper);
		}
		
		public List<UserEntity> findAllUsers() {
				QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<UserEntity>();
				return userDao.selectList(queryWrapper);
		}
		
		@Transactional(rollbackFor = Exception.class)
		public UserResponse deleteUserById(Integer userId) {
				String status;
				UserEntity user = userDao.selectById(userId);
				int result = userDao.deleteById(userId);
				if (result>0){
						status = Constants.responseOk;
				}else {
						status = Constants.responseError;
				}
				return UserResponse.buildUserResponse(status,user);
		}
		
		@Transactional(rollbackFor = Exception.class)
		public UserResponse updateUser(UserEntity userEntity) {
				String status;
				String msg;
				int result = userDao.updateById(userEntity);
				if (result>0){
						status = Constants.responseOk;
						msg = "更新用户成功!";
				}else {
						status = Constants.responseError;
						msg = "更新用户失败!";
				}
				return UserResponse.buildUserResponse(status,msg);
		}
		
		public UserEntity findUserById(int userId) {
				return userDao.selectById(userId);
		}
		
		public List<UserEntity> blurSelectUsers(String queryParam,int condition) {
				QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<UserEntity>();
				switch (condition){
						case 2:
								queryWrapper.like(!StringUtils.isEmpty(queryParam),"ruoqing_user_username",queryParam);
								break;
						case 3:
								queryWrapper.like(!StringUtils.isEmpty(queryParam),"ruoqing_user_phonenumber",queryParam);
								break;
						case 4:
								queryWrapper.like(!StringUtils.isEmpty(queryParam),"ruoqing_user_qqnumber",queryParam);
								break;
						case 5:
								queryWrapper.like(!StringUtils.isEmpty(queryParam),"ruoqing_user_wechat",queryParam);
								break;
						default:
						log.error("无效模糊查询参数"+condition);
				}
				return userDao.selectList(queryWrapper);
		}
		
		public Page<UserEntity> selectUserByPagination(Integer currentPage,Integer pageSize) {
				QueryWrapper<UserEntity> wrapper = new QueryWrapper<UserEntity>();
				Page<UserEntity> page = new Page<UserEntity>(currentPage, pageSize);
				return userDao.selectPage(page,wrapper);
		}
		
		public BaseResponse deleteUserBySelect(String queryParam, UserQueryEnum userQueryEnum) {
				String status;
				String msg;
				UserEntity userEntity = userService.findUserByCondition(queryParam, userQueryEnum);
				if (ObjectUtils.isNull(userEntity)){
						status = Constants.responseError;
						msg = "用户不存在";
				}else {
						userService.deleteUserById(userEntity.getUserId());
						status = Constants.responseOk;
						msg = "删除用户成功!";
				}
				return BaseResponse.buildResponse(status,msg);
		}
		
		public BaseResponse addUser(UserEntity userEntity) {
				userDao.insert(userEntity);
				return BaseResponse.buildResponse(Constants.responseOk);
		}
		
}
